/*
**  HGE-X
**
**  (c) curepine 2011
*/
#include "hge_impl.h"
#include <XInput.h>

#define MAX_USERS 4

void HGE_Impl::_InitXInput()
{
    XInputEnable(TRUE);
}

void CALL HGE_Impl::Gamepad_GetState(int player, hgeGamepad *pad)
{
    if (pad == NULL)
        return;
    if (player < 0 || player >= MAX_USERS)
        return;
    XINPUT_STATE xpad;
    if (ERROR_DEVICE_NOT_CONNECTED == XInputGetState(player, &xpad))
    {
        pad->connected = false;
        return;
    }
    pad->connected = true;
    pad->buttons = xpad.Gamepad.wButtons;
    pad->leftTrigger = xpad.Gamepad.bLeftTrigger / 255.0f;
    pad->rightTrigger = xpad.Gamepad.bRightTrigger / 255.0f;
    pad->leftThumbX = xpad.Gamepad.sThumbLX / 32768.0f;
    pad->leftThumbY = xpad.Gamepad.sThumbLY / 32768.0f;
    pad->rightThumbX = xpad.Gamepad.sThumbRX / 32768.0f;
    pad->rightThumbY = xpad.Gamepad.sThumbRY / 32768.0f;
}

void CALL HGE_Impl::Gamepad_Vibration(int player, int leftMotor, int rightMotor)
{
    if (player < 0 || player >= MAX_USERS)
        return;
    XINPUT_VIBRATION vibration;
    vibration.wLeftMotorSpeed = leftMotor;
    vibration.wRightMotorSpeed = rightMotor;
    XInputSetState(player, &vibration);
}